package com.hero.jobweb.action.tag;

import static com.hero.jobweb.config.CacheData.getMultiName;

import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.Component;

import com.hero.basicsystem.entity.Code;
import com.hero.jobweb.config.CacheData;
import com.opensymphony.xwork2.util.ValueStack;

public class JobWebShowCodeComponent extends Component {
	private static final Log log = LogFactory
			.getLog(JobWebShowCodeComponent.class);

	private String levelSep = "--";// 有上下级关系的分隔符标志
	private boolean showParentName = false;// 默认显示上级名称

	public String getLevelSep() {
		return levelSep;
	}

	public void setLevelSep(String levelSep) {
		this.levelSep = levelSep;
	}

	public boolean isShowParentName() {
		return showParentName;
	}

	public void setShowParentName(boolean showParentName) {
		this.showParentName = showParentName;
	}

	private String mapMethodName;

	public String getMapMethodName() {
		return mapMethodName;
	}

	public void setMapMethodName(String mapMethodName) {
		this.mapMethodName = mapMethodName;
	}

	private String code;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
		code = stripExpressionIfAltSyntax(getCode());// 计算表达式
		String actualValue = null;
		actualValue = (String) getStack().findValue(code, String.class,
					throwExceptionOnELFailure);// 堆栈中查找值
		if (StringUtils.isEmpty(actualValue)) {
			actualValue = this.code;
		}
		setCodeValue(actualValue);
	}

	public JobWebShowCodeComponent(ValueStack stack) {
		super(stack);
	}

	private String codeValue;

	public String getCodeValue() {
		return codeValue;
	}

	public void setCodeValue(String codeValue) {
		this.codeValue = codeValue;
	}

	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			
			Method getMapMethod=ClassUtils.getPublicMethod(CacheData.class, getMapMethodName(), new Class[] {});
			@SuppressWarnings("unchecked")
			Map<String, Code> codeMap = (Map<String, Code>) getMapMethod
					.invoke(CacheData.class, new Object[] {});// 获取Map
			String codeName;
			codeName = getMultiName(codeMap, getCodeValue(),showParentName,levelSep);
			writer.write(codeName);
		} catch (Exception e) {
			log.warn(e.getMessage());
		}
		return result;
	}
}
